<template>
  <view class="">
    <my-search @click="gotoSearch"></my-search>

    <view class="scroll-view-container">

      <!-- 左边的区域 -->
      <scroll-view scroll-y="true" class="left-scroll-view" :style="{height: wh+'px',width: '130px'}">
        <block v-for="(item,index) in cateList" :key="index">
          <view :class="['left-scroll-view-item',index === active ? 'active':'']" @click="active = index">
            {{item.cat_name}}
          </view>
        </block>
      </scroll-view>
      <!-- 右边的区域 -->
      <scroll-view scroll-y="true" class="right-scroll-view" :style="{height: wh + 'px'}" :scroll-top="scrollTop">
        <!-- 二级分类 -->
        <view class="cate-lv2" v-for="(item2,i2) in cateLevel2" :key="i2">
          <view class="cate-lv2-title">/{{item2.cat_name}}/</view>
          <view class="cate-lv3-list">
            <view class="cate-lv3-item" v-for="(item3,i3) in item2.children" :key="i3" @click="gotoGoodsList(item3)">
              <image :src="item3.cat_icon" mode="widthFix"></image>
              <text>{{item3.cat_name}}</text>
            </view>
          </view>
        </view>
      </scroll-view>
    </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        // 手机的可视高度
        // 窗口的可用高度 = 屏幕高度 - navigationBar高度 - tabBar 高度
        wh: 0,
        // 分类列表
        cateList: [],
        // 激活索引
        active: 0,
        // 二级分类
        cateLevel2: [],
        // 顶部位置
        scrollTop: 0
      };
    },
    onLoad() {
      const info = uni.getSystemInfoSync()
      this.wh = info.windowHeight
      this.getCateList()
    },
    methods: {
      /**
       * 获取分类列表
       */
      async getCateList() {
        const {
          data: res
        } = await uni.$http.get('/api/public/v1/categories');
        if (res.meta.status !== 200) {
          return uni.$showMsg()
        }
        this.cateList = res.message
        this.cateLevel2 = res.message[this.active].children
      },
      gotoGoodsList(item) {
        uni.navigateTo({
          url: '/subpkg/goods_list/goods_list?cid=' + item.cat_id
        })
      },
      gotoSearch() {
        uni.navigateTo({
          url:'/subpkg/search/search'
        })
      }
    },
    watch: {
      active(newActive) {
        this.cateLevel2 = this.cateList[this.active].children
        this.scrollTop = this.scrollTop === 0 ? 1 : 0
      }
    }
  }
</script>

<style lang="scss">
  .scroll-view-container {
    display: flex;

    .left-scroll-view-item {
      line-height: 60px;
      background-color: #f7f7f7;
      text-align: center;
      font-size: 12px;
      box-sizing: border-box;

      &.active {
        background: #FFF;
        border-left: 3px solid #C00;
      }

    }

    .cate-lv2-title {
      text-align: center;
      font-weight: 700;
      font-size: 12px;
      padding: 15px 0;
    }

    .cate-lv3-list {
      display: flex;
      flex-wrap: wrap;

      .cate-lv3-item {
        width: 33%;
        display: flex;
        flex-direction: column;
        margin-bottom: 10px;
        align-items: center;

        image {
          width: 60px;
        }

        text {
          font-size: 12px;
        }
      }
    }
  }
</style>